package com.kizuki.algorithm.leetcode;

/**
 * 给你两个单链表的头节点 headA 和 headB ，请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点，返回 null 。
 *
 * https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/
 */
public class 相交链表 {

    class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            if (headA == null || headB == null) {
                return null;
            }
            ListNode hA = headA, hB = headB;
            while(hA != hB) {
                if (hA == null) {
                    hA = headB;
                } else {
                    hA = hA.next;
                }
                if (hB== null) {
                    hB = headA;
                } else {
                    hB = hB.next;
                }
            }
            return hA;
        }
    }

}